﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;
using Lamex.mTube.Properties;
using Lamex.Library.CommonClass;
using Lamex.Interface;
using System.IO;

namespace Lamex.mTube.UI
{
    public partial class frmTube_Layout : frmBaseChild
    {
        public frmTube_Layout()
        {
            InitializeComponent();

            this.panel1.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
            this.pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
            this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
        }

        private void frmTube_Layout_Load(object sender, EventArgs e)
        {
            //pictureBox1.Image = Resources.Tube_Layout;
            //pictureBox1.Image = Image.FromFile(@"\\Tx-fs10\tx-fs1\LAMEX\MES\SysSource\Flow\Tube_Layout.png");
            //pictureBox1.Image = new Bitmap(@"\\Tx-fs10\tx-fs1\LAMEX\MES\SysSource\Flow\Tube_Layout.png");
            AddImage("Tube_Layout.png");
        }

        private void sbtnLayout_Click(object sender, EventArgs e)
        {
            //pictureBox1.Image = Resources.Tube_Layout;
            //pictureBox1.Image = Image.FromFile(@"\\Tx-fs10\tx-fs1\LAMEX\MES\SysSource\Flow\Tube_Layout.png");
            AddImage("Tube_Layout.png");
        }

        private void sbtnFlowInfo_Click(object sender, EventArgs e)
        {
            //pictureBox1.Image = Resources.Tube_Info;
            //pictureBox1.Image = Image.FromFile(@"\\Tx-fs10\tx-fs1\LAMEX\MES\SysSource\Flow\Tube_Info.png");
            AddImage("Tube_Info.png");
        }

        private void sbtnFlowDetail_Click(object sender, EventArgs e)
        {
            //pictureBox1.Image = Resources.Tube_Flow;
            //pictureBox1.Image = Image.FromFile(@"\\Tx-fs10\tx-fs1\LAMEX\MES\SysSource\Flow\Tube_Flow.png");
            AddImage("Tube_Flow.png");
        }

        //文件流加载图片，避免占用文件资源，并减少内存占用
        private void AddImage(string strFileName)
        {
            string strPath = @"\\Tx-fs10\tx-fs1\LAMEX\MES\SysSource\Flow\";
            FileStream fileStream = new FileStream(strPath + strFileName, FileMode.Open, FileAccess.Read);
            int byteLength = (int)fileStream.Length;
            byte[] fileBytes = new byte[byteLength];
            fileStream.Read(fileBytes, 0, byteLength);
            //文件流关闭,文件解除锁定
            fileStream.Close();
            //ImageList1.Images.Add(Image.FromStream(new MemoryStream(fileBytes)));
            pictureBox1.Image = Image.FromStream(new MemoryStream(fileBytes));
        }

        private void frmTube_Layout_Resize(object sender, EventArgs e)
        {
            pictureBox1.Width = panel1.Width;
            pictureBox1.Height = panel1.Height;
            //pictureBox1.Load(@"\\Tx -fs10\tx-fs1\LAMEX\MES\Resources\MES_Operation.jpg");//白底
            //pictureBox1.Load(@"\\Tx-fs10\tx-fs1\LAMEX\MES\Resources\MES_Operation.png");//皮肤底
        }

        #region 图片放大缩小平移

        private bool isMouseDown = false;
        private Point mouseOffset;

        private void panel1_MouseWheel(object sender, MouseEventArgs e)
        {
            double step = 1.2;
            if (e.Delta > 0)
            {
                if (pictureBox1.Height >= Screen.PrimaryScreen.Bounds.Height * 10)
                    return;
                pictureBox1.Height = (int)(pictureBox1.Height * step);
                pictureBox1.Width = (int)(pictureBox1.Width * step);

                int px = Cursor.Position.X - pictureBox1.Location.X;
                int py = Cursor.Position.Y - pictureBox1.Location.Y;
                int px_add = (int)(px * (step - 1.0));
                int py_add = (int)(py * (step - 1.0));
                pictureBox1.Location = new Point(pictureBox1.Location.X - px_add, pictureBox1.Location.Y - py_add);
                Application.DoEvents();
            }
            else
            {
                pictureBox1.Height = (int)(pictureBox1.Height / step);
                pictureBox1.Width = (int)(pictureBox1.Width / step);

                int px = Cursor.Position.X - pictureBox1.Location.X;
                int py = Cursor.Position.Y - pictureBox1.Location.Y;
                int px_add = (int)(px * (1.0 - 1.0 / step));
                int py_add = (int)(py * (1.0 - 1.0 / step));
                pictureBox1.Location = new Point(pictureBox1.Location.X + px_add, pictureBox1.Location.Y + py_add);
                Application.DoEvents();
            }
        }

        private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            double step = 1.2;
            if (e.Delta > 0)
            {
                if (pictureBox1.Height >= Screen.PrimaryScreen.Bounds.Height * 10)
                    return;
                pictureBox1.Height = (int)(pictureBox1.Height * step);
                pictureBox1.Width = (int)(pictureBox1.Width * step);

                int px = Cursor.Position.X - pictureBox1.Location.X;
                int py = Cursor.Position.Y - pictureBox1.Location.Y;
                int px_add = (int)(px * (step - 1.0));
                int py_add = (int)(py * (step - 1.0));
                pictureBox1.Location = new Point(pictureBox1.Location.X - px_add, pictureBox1.Location.Y - py_add);
                Application.DoEvents();
            }
            else
            {
                pictureBox1.Height = (int)(pictureBox1.Height / step);
                pictureBox1.Width = (int)(pictureBox1.Width / step);

                int px = Cursor.Position.X - pictureBox1.Location.X;
                int py = Cursor.Position.Y - pictureBox1.Location.Y;
                int px_add = (int)(px * (1.0 - 1.0 / step));
                int py_add = (int)(py * (1.0 - 1.0 / step));
                pictureBox1.Location = new Point(pictureBox1.Location.X + px_add, pictureBox1.Location.Y + py_add);
                Application.DoEvents();
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown)
            {
                int left = pictureBox1.Left + e.X - mouseOffset.X;
                int top = pictureBox1.Top + e.Y - mouseOffset.Y;
                pictureBox1.Location = new Point(left, top);
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMouseDown = false;
            }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOffset.X = e.X;
                mouseOffset.Y = e.Y;
                isMouseDown = true;
            }
            else if (e.Button == MouseButtons.Right)//右键（右击）
            {
                pictureBox1.Width = panel1.Width;
                pictureBox1.Height = panel1.Height;
                pictureBox1.Location = new Point(0, 0);
            }
        }

        #endregion

        private void sbtnPlanning_Click(object sender, EventArgs e)
        {
            MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmTube_Demo), sender as ToolStripMenuItem);
        }

        private void sbtnLaserCutting_Click(object sender, EventArgs e)
        {
            MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmTube_Demo), sender as ToolStripMenuItem);
        }

        private void sbtnAutoWelding_Click(object sender, EventArgs e)
        {
            MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmTube_Demo), sender as ToolStripMenuItem);
        }

        private void sbtnPackaging_Click(object sender, EventArgs e)
        {
            //MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmTube_Packaging), sender as ToolStripMenuItem);
            MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmTube_Digital_Display), sender as ToolStripMenuItem);            
        }
    }
}